
package com.seekting.study;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class FutureTaskStudy {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newSingleThreadExecutor();

        Callable<String> callable = new Callable<String>() {

            @Override
            public String call() throws Exception {
                System.out.println("call start");
                TimeUnit.SECONDS.sleep(3);
                System.out.println("call end");
                return "xxx";
            }
        };
        FutureTask<String> futureTask = new FutureTask<String>(callable);
        executor.submit(futureTask);
        FutureTask<String> futureTask2 = new FutureTask<String>(callable);
        executor.submit(futureTask2);
        TimeUnit.SECONDS.sleep(1);
        futureTask.cancel(false);
        futureTask2.cancel(false);
        executor.shutdown();
        while (!executor.isTerminated()) {
        }

        System.out.println("over");
    }
}
